{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from VecSim import *\n",
    "import numpy as np\n",
    "import hnswlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dim = 16\n",
    "num_elements = 10000\n",
    "space = 'l2'\n",
    "M=16\n",
    "efConstruction = 100\n",
    "efRuntime = 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Build RedisLabs VecSim HNSWLib Index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "params = VecSimParams()\n",
    "hnswparams = HNSWParams()\n",
    "\n",
    "params.algo = VecSimAlgo_HNSWLIB\n",
    "\n",
    "hnswparams.dim = dim\n",
    "hnswparams.metric = VecSimMetric_L2\n",
    "hnswparams.type = VecSimType_FLOAT32\n",
    "hnswparams.M = M\n",
    "hnswparams.efConstruction = efConstruction\n",
    "hnswparams.initialCapacity = num_elements\n",
    "hnswparams.efRuntime=efRuntime\n",
    "\n",
    "params.hnswParams = hnswparams\n",
    "index = VecSimIndex(params)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Build HNSWLib index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = hnswlib.Index(space='l2', dim=dim)\n",
    "p.init_index(max_elements=num_elements, ef_construction=efConstruction, M=M)\n",
    "p.set_ef(efRuntime)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.float32(np.random.random((num_elements, dim)))\n",
    "for i, vector in enumerate(data):\n",
    "    index.add_vector(vector, i)\n",
    "    p.add_items(vector, i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "test_index (__main__.TestNotebook) ... ok\n",
      "\n",
      "----------------------------------------------------------------------\n",
      "Ran 1 test in 0.790s\n",
      "\n",
      "OK\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<unittest.main.TestProgram at 0x7fcda8405df0>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import unittest\n",
    "from  numpy.testing import assert_array_equal\n",
    "\n",
    "class TestNotebook(unittest.TestCase):\n",
    "    \n",
    "    def test_index(self):\n",
    "        for vector in data:\n",
    "            hnswlib_labels, hnswlib_distances = p.knn_query(vector, k=10)\n",
    "            redis_labels, redis_distances = index.knn_query(vector, 10)\n",
    "            assert_array_equal(hnswlib_labels, redis_labels)\n",
    "            assert_array_equal(hnswlib_distances, redis_distances)\n",
    "        \n",
    "\n",
    "unittest.main(argv=[''], verbosity=2, exit=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
